package com.edu.etu.gui.checkit;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;

public class ViewTemplateActivity extends ActionBarActivity implements View.OnClickListener{

    SQLiteHelperList openHelperList;
    SQLiteDatabase db;
    ContentValues cv;
    Integer idElement;
    LinearLayout main;

    TextView NameTemplate;
    int idThisTemplate=0;
    private HashMap<View,Integer> mapViewTemplate = new HashMap<View,Integer>();
    int wrapContent = LinearLayout.LayoutParams.WRAP_CONTENT;
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_template);

        openHelperList = new SQLiteHelperList(getApplicationContext());
        db = openHelperList.getWritableDatabase();
        cv = new ContentValues();

        LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(wrapContent, wrapContent);
        lParams.gravity = Gravity.LEFT;

        ImageButton openEditActivity = (ImageButton)findViewById(R.id.openEditActivityT);
        openEditActivity.setOnClickListener(this);

        ImageButton btnDelAllTemplate = (ImageButton) findViewById(R.id.btnDelAllTemplate);
        btnDelAllTemplate.setOnClickListener(this);

        main=(LinearLayout)findViewById(R.id.mainListTemplate);
        NameTemplate = (TextView)findViewById(R.id.template_name);

        cursor = db.query("ListsTemplates", null, null, null, null, null, null);
        if(cursor.moveToFirst())
        {
            do {
                if(cursor.getInt(cursor.getColumnIndex("_id4"))==getIntent().getIntExtra("TemplateId",-1))
                {
                    idThisTemplate = cursor.getInt(cursor.getColumnIndex("_id4"));
                    NameTemplate.setText(cursor.getString(cursor.getColumnIndex("nameListsTemplates")));
                }
            }
            while (cursor.moveToNext());
        }
        cursor.close();

        cursor = db.query("Template", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            int ListsTemplates = cursor.getColumnIndex("ListsTemplates");
            int nameElementsTemplate = cursor.getColumnIndex("nameElementsTemplate");
            int boolTemplate = cursor.getColumnIndex("boolTemplate");
            int idElement = cursor.getColumnIndex("_id3");
            do {
                if(getIntent().getIntExtra("TemplateId",-1)==cursor.getInt(ListsTemplates))
                {
                    LayoutInflater inflater = getLayoutInflater();
                    View list_element = inflater.inflate(R.layout.view_template,null,false);
                    TextView element_checkbox =  (TextView)list_element.findViewById(R.id.text_View_template);
                    element_checkbox.setText(cursor.getString(nameElementsTemplate));
                    main.addView(list_element);
                    mapViewTemplate.put(element_checkbox, cursor.getInt(idElement));
                    element_checkbox.setOnClickListener(this);
                }
            }
            while (cursor.moveToNext());
        }
        cursor.close();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.view_template, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.viewCheckBoxElement:
                idElement=mapViewTemplate.get(v);
                if(idElement!=null)
                {
                    CheckBox cb = (CheckBox)v;
                    cursor = db.query("Template", null, null, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        do {
                            if(idElement==cursor.getInt(cursor.getColumnIndex("_id2")))
                            {
                                String strFilter = "_id3="+idElement;
                                ContentValues args = new ContentValues();
                                args.put(openHelperList.boolTemplate,cb.isChecked());
                                db.update(openHelperList.NameTableTemplate,args,strFilter,null);
                            }
                        }
                        while (cursor.moveToNext());
                    }
                    cursor.close();
                }
                break;
            case R.id.openEditActivityT:
                Intent intent = new Intent(this, EditTemplateActivity.class);
                intent.putExtra("TemplateId",getIntent().getIntExtra("TemplateId",-1));
                startActivity(intent);
                break;
            case R.id.btnDelAllTemplate:
                cursor = db.query("ListsTemplates", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        if(cursor.getInt(cursor.getColumnIndex("_id4"))==idThisTemplate)
                        {
                            String i = cursor.getString(cursor.getColumnIndex("_id4"));
                            if (i.equalsIgnoreCase("")) {
                                break;
                            }
                            db.execSQL("delete from ListsTemplates where _id4='"+i+"'");
                        }
                    }
                    while (cursor.moveToNext());
                }
                cursor.close();

                cursor = db.query("Template", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        if(idThisTemplate==cursor.getInt(cursor.getColumnIndex("ListsTemplates")))
                        {
                            String i = cursor.getString(cursor.getColumnIndex("_id3"));
                            if (i.equalsIgnoreCase("")) {
                                break;
                            }
                            db.execSQL("delete from Template where _id3='"+i+"'");
                        }
                    }
                    while (cursor.moveToNext());
                }
                cursor.close();
                Intent finish = new Intent(this, Template.class);
                startActivity(finish);
                finish();
                break;
        }
    }
}
